package com.xxxx.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.mapper.DepartmentMapper;
import com.xxxx.pojo.Department;
import com.xxxx.pojo.R;
import com.xxxx.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chao
 * @since 2022-09-26
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 获取所有部门列表
     * @return
     */
    @Override
    public List<Department> getAllDepartmentList() {

        return departmentMapper.getAllDepartmentList(-1);
    }

    /**
     * 添加部门
     * @param department
     * @return
     */
    @Override
    public R saveDepartment(Department department) {
        department.setEnabled(true);
        departmentMapper.addDep(department);
        if (1 == department.getResult()) {
            return R.success("添加成功！",department);
        }
        return R.error("添加失败！");
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @Override
    public R deleteDepartment(Integer id) {
        Department department = new Department();
        department.setId(id);
        departmentMapper.deleteDep(department);
        if (-2 == department.getResult()) {
            return R.error("该部门下还有子部门，删除失败！");
        }
        if (-1 == department.getResult()) {
            return R.error("该部门下还有员工，删除失败！");
        }
        if (department.getResult() == 1) {
            return R.success("删除成功！");
        }
        return R.error("删除失败！");
    }
}
